package com.duleme.servlet;

import com.duleme.common.EncryptUtil.MD5EncryptUtils;
import com.duleme.common.EncodeUtils;
import com.duleme.entity.Shop;
import com.duleme.entity.User;
import com.duleme.entity.UserInfo;
import com.duleme.service.UserService;
import com.duleme.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "UserLoginServlet",urlPatterns = "/User/LoginServlet")
public class UserLoginServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        String userAccount=request.getParameter("userAccount");
        String password=request.getParameter("userPassword");
        User userObject = (User) request.getSession().getAttribute("user");
        if (userObject==null||userObject.getUserAccount() != userAccount){
            UserService userService=new UserServiceImpl();
            System.out.println(userAccount+":"+password);
            User user=userService.login(userAccount, MD5EncryptUtils.md5Encrypt(password));
            PrintWriter writer=response.getWriter();
            //request.setAttribute("user",user);
            if(null!=user){
                Map<String,String> map = new HashMap<>();
                map.put("result","1");
                map.put("userIdentify",String.valueOf(user.getIdentity()));
                request.getSession().setAttribute("user",user);
                UserInfo userInfo = ((UserServiceImpl) userService).getUserInfo(userAccount, MD5EncryptUtils.md5Encrypt(password));
                request.getSession().setAttribute("userInfo",userInfo);
                System.out.println(userInfo.getId());
                if (user.getIdentity()==1){
                    Shop shop = userService.getShop(user.getUserId());
                    if (shop == null){
                        map.put("shop","2");

                    }else {
                        map.put("shop","1");
                        request.getSession().setAttribute("shop",shop);
                    }
                }
                String json = new Gson().toJson(map);
                writer.print(json);//登陆成功
                System.out.println(json);
                request.getSession().setAttribute("login","1");
                //request.getSession().setAttribute("text","test");
            }else{
                writer.print("0");//登陆失败
                System.out.println("0");
            }
            writer.close();
        }else {
            PrintWriter writer=response.getWriter();
            writer.print("2");
            System.out.println("2");
            writer.close();
        }


    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
    }
}
